<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="http-会话机制" tabindex="-1"><a class="header-anchor" href="#http-会话机制"><span>HTTP 会话机制</span></a></h2>
<ul>
<li><a href="#introduction">简介</a>
<ul>
<li><a href="#configuration">配置</a></li>
<li><a href="#driver-prerequisites">驱动程序先决条件</a></li>
</ul>
</li>
<li><a href="#interacting-with-the-session">使用 Session</a>
<ul>
<li><a href="#retrieving-data">获取数据</a></li>
<li><a href="#storing-data">存储数据</a></li>
<li><a href="#flash-data">闪存数据</a></li>
<li><a href="#deleting-data">删除数据</a></li>
<li><a href="#regenerating-the-session-id">重新生成 Session ID</a></li>
</ul>
</li>
<li><a href="#session-blocking">Session Blocking</a></li>
<li><a href="#adding-custom-session-drivers">添加自定义 Session 驱动</a>
<ul>
<li><a href="#implementing-the-driver">实现驱动</a></li>
<li><a href="#registering-the-driver">注册驱动</a></li>
</ul>
</li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>由于 HTTP 驱动的应用程序是无状态的，Session 提供了一种在多个请求之间存储有关用户信息的方法，这类信息一般都存储在后续请求可以访问的持久存储 / 后端中。</p>
<p>Laravel 通过同一个可读性强的 API 处理各种自带的后台驱动程序。支持诸如比较热门的 <a href="https://memcached.org/" target="_blank" rel="noopener noreferrer">Memcached</a>、 <a href="https://redis.io/" target="_blank" rel="noopener noreferrer">Redis</a> 和数据库。</p>
<h3 id="配置" tabindex="-1"><a class="header-anchor" href="#配置"><span>配置</span></a></h3>
<p>Session 的配置文件存储在 <code v-pre>config/session.php</code> 文件中。请务必查看此文件中对于你而言可用的选项。默认情况下，Laravel 配置的 Session 驱动为 <code v-pre>database</code>。</p>
<p>Session <code v-pre>driver</code> 的配置预设了每个请求存储 Session 数据的位置。Laravel 自带了几个不错而且开箱即用的驱动：</p>
<ul>
<li><code v-pre>file</code> - Sessions 存储在 <code v-pre>storage/framework/sessions</code> 中。</li>
<li><code v-pre>cookie</code> - Sessions 被存储在安全加密的 cookie 中。</li>
<li><code v-pre>database</code> - Sessions 被存储在关系型数据库中。</li>
<li><code v-pre>memcached</code> / <code v-pre>redis</code> - Sessions 被存储在基于高速缓存的存储系统中。</li>
<li><code v-pre>dynamodb</code> - Sessions 被存储在 AWS DynamoDB 中。</li>
<li><code v-pre>array</code> - Sessions 存储在 PHP 数组中，但不会被持久化。</li>
</ul>
<blockquote>
<p>技巧：数组驱动一般用于 <a href="https://learnku.com/docs/laravel/11.x/testing" target="_blank" rel="noopener noreferrer">测试</a> 并且防止存储在 Session 中的数据被持久化。</p>
</blockquote>
<h3 id="驱动程序先决条件" tabindex="-1"><a class="header-anchor" href="#驱动程序先决条件"><span>驱动程序先决条件</span></a></h3>
<h4 id="数据库" tabindex="-1"><a class="header-anchor" href="#数据库"><span>数据库</span></a></h4>
<p>当使用 <code v-pre>database</code> 会话驱动程序时，您需要确保有一个数据库表来存储会话数据。通常，这在 Laravel 默认的 <code v-pre>0001_01_01_000000_create_users_table.php</code> <a href="https://learnku.com/docs/laravel/11.x/migrations" target="_blank" rel="noopener noreferrer">数据库迁移</a> 中已包含；然而，如果出于任何原因您没有 <code v-pre>sessions</code> 数据表，您可以使用 <code v-pre>make:session-table</code> Artisan 命令生成这个迁移：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan make:session-table</span>
<span class="line"></span>
<span class="line">php artisan migrate</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="redis" tabindex="-1"><a class="header-anchor" href="#redis"><span>Redis</span></a></h4>
<p>在使用 Laravel 的 Redis 会话之前，您需要通过 PECL 安装 <code v-pre>PhpRedis</code> PHP 扩展或者通过 Composer 安装 <code v-pre>predis/predis</code> 包（版本 ~1.0）。有关配置 Redis 的更多信息，请参阅 Laravel 的 <a href="https://learnku.com/docs/laravel/11.x/redis#configuration" target="_blank" rel="noopener noreferrer">Redis 文档</a> 。</p>
<blockquote>
<p><strong>注意</strong><br>
您可以使用 <code v-pre>SESSION_CONNECTION</code> 环境变量，或者在 <code v-pre>session.php</code> 配置文件中的 <code v-pre>connection</code> 选项来指定用于会话存储的 Redis 连接。</p>
</blockquote>
<h2 id="使用-session" tabindex="-1"><a class="header-anchor" href="#使用-session"><span>使用 Session</span></a></h2>
<h3 id="获取数据" tabindex="-1"><a class="header-anchor" href="#获取数据"><span>获取数据</span></a></h3>
<p>在 Laravel 中，处理会话数据有两种主要方法：全局的 <code v-pre>session</code> 辅助函数和通过 <code v-pre>Request</code> 实例。首先，让我们看一下如何通过 <code v-pre>Request</code> 实例访问会话数据，在路由闭包或控制器方法中可以对其进行类型提示。请记住，控制器方法的依赖项会通过 Laravel 的 <a href="https://learnku.com/docs/laravel/11.x/container" target="_blank" rel="noopener noreferrer">服务容器</a> 自动注入：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>View<span class="token punctuation">\</span>View</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">UserController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 显示给定用户的个人资料。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">show</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$id</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">View</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$value</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line">        <span class="token variable">$user</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">users</span><span class="token operator">-></span><span class="token function">find</span><span class="token punctuation">(</span><span class="token variable">$id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user.profile'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token variable">$user</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当您从 Session 中检索一个项目时，您也可以将默认值作为第二个参数传递给 <code v-pre>get</code> 方法。如果指定的键在 Session 中不存在，将返回这个默认值。如果您将闭包作为默认值传递给 <code v-pre>get</code> 方法，且请求的键不存在，闭包将被执行并返回其结果：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token string single-quoted-string">'default'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="全局-session-辅助函数" tabindex="-1"><a class="header-anchor" href="#全局-session-辅助函数"><span>全局 Session 辅助函数</span></a></h4>
<p>您也可以使用全局 <code v-pre>session</code> PHP 函数来检索和存储 Session 中的数据。当使用单个字符串参数调用 <code v-pre>session</code> 辅助函数时，它将返回该 Session 键的值。当使用键值对数组调用该辅助函数时，这些值将被存储在 Session 中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/home'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 从 Session 中检索一条数据...</span></span>
<span class="line">    <span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">session</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 指定默认值...</span></span>
<span class="line">    <span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">session</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 在 Session 中存储一条数据...</span></span>
<span class="line">    <span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>技巧</strong><br>
通过 HTTP 请求实例使用 session 与使用全局 <code v-pre>session</code> 辅助函数之间几乎没有实际差异。 这两种方法 <a href="https://learnku.com/docs/laravel/11.x/testing" target="_blank" rel="noopener noreferrer">testable</a> 都可以通过 <code v-pre>assertSessionHas</code> 方法进行测试，该方法在所有测试用例中都可用。</p>
</blockquote>
<h4 id="检索所有-session-数据" tabindex="-1"><a class="header-anchor" href="#检索所有-session-数据"><span>检索所有 Session 数据</span></a></h4>
<p>如果您想检索 Session 中的所有数据，可以使用 <code v-pre>all</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="获取-session-数据的一部分" tabindex="-1"><a class="header-anchor" href="#获取-session-数据的一部分"><span>获取 Session 数据的一部分</span></a></h4>
<p>可以使用 <code v-pre>only</code> 和 <code v-pre>except</code> 方法来获取会话数据的一部分：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">only</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'username'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">except</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'username'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="确定-session-中是否存在某项" tabindex="-1"><a class="header-anchor" href="#确定-session-中是否存在某项"><span>确定 Session 中是否存在某项</span></a></h4>
<p>要确定 Session 中是否存在某项，你可以使用 <code v-pre>has</code> 方法。如果该项存在且不为 <code v-pre>null</code> ，<code v-pre>has</code> 方法将返回 <code v-pre>true</code>。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">has</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要判断 Session 中是否存在某项，即使该项的值为 <code v-pre>null</code> ，你可以使用 <code v-pre>exists</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要判断 Session 中不存在某项时，你可以使用 <code v-pre>missing</code> 方法。如果该项不存在，<code v-pre>missing</code> 方法将返回 <code v-pre>true</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">missing</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="存储数据" tabindex="-1"><a class="header-anchor" href="#存储数据"><span>存储数据</span></a></h3>
<p>Session 里存储数据，你通常将使用 Request 实例中的 <code v-pre>put</code> 方法或者 <code v-pre>session</code> 助手函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 通过 Request 实例存储 ...</span></span>
<span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 通过全局 Session 助手函数存储 ...</span></span>
<span class="line"><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="向-session-数组中添加元素" tabindex="-1"><a class="header-anchor" href="#向-session-数组中添加元素"><span>向 Session 数组中添加元素</span></a></h4>
<p><code v-pre>push</code> 方法可以把一个新值推入到以数组形式存储的 session 值里。例如：如果 <code v-pre>user.teams</code> 键值对有一个关于团队名字的数组，你可以推入一个新值到这个数组里：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user.teams'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'developers'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="检索并删除项目" tabindex="-1"><a class="header-anchor" href="#检索并删除项目"><span>检索并删除项目</span></a></h4>
<p><code v-pre>pull</code> 方法将在一个语句中从 Session 中检索并删除一个项目:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">pull</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="递增和递减会话值" tabindex="-1"><a class="header-anchor" href="#递增和递减会话值"><span>递增和递减会话值</span></a></h4>
<p>如果您的 Session 数据包含一个您希望递增或递减的整数，您可以使用 <code v-pre>increment</code> 和 <code v-pre>decrement</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">increment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'count'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">increment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'count'</span><span class="token punctuation">,</span> <span class="token variable">$incrementBy</span> <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">decrement</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'count'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">decrement</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'count'</span><span class="token punctuation">,</span> <span class="token variable">$decrementBy</span> <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="闪存数据" tabindex="-1"><a class="header-anchor" href="#闪存数据"><span>闪存数据</span></a></h3>
<p>有时您可能希望将项目存储在 Session 中供下一个请求使用。您可以使用 <code v-pre>flash</code> 方法来实现这一点。使用此方法存储在 Session 中的数据将立即可用，并在随后的 HTTP 请求中仍然可用。在随后的 HTTP 请求之后，闪存数据将被删除。闪存数据主要用于短期的状态消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">flash</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'status'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Task was successful!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果您需要在多个请求中保持闪存数据，可以使用 <code v-pre>reflash</code> 方法，它会将所有闪存数据保留到下一个请求。如果您只需要保留特定的闪存数据，可以使用 <code v-pre>keep</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">reflash</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">keep</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'username'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要仅在当前请求中保留您的闪存数据，您可以使用 <code v-pre>now</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">now</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'status'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Task was successful!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="删除数据" tabindex="-1"><a class="header-anchor" href="#删除数据"><span>删除数据</span></a></h3>
<p><code v-pre>forget</code> 方法将从 Session 中删除一条数据。如果您想删除 Session 中的所有数据，可以使用 <code v-pre>flush</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 删除一个单独的键值对 ...</span></span>
<span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">forget</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 删除多个键值对 ...</span></span>
<span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">forget</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'status'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="重新生成-session-id" tabindex="-1"><a class="header-anchor" href="#重新生成-session-id"><span>重新生成 Session ID</span></a></h3>
<p>重新生成会话 ID 通常是为了防止恶意用户利用 <a href="https://owasp.org/www-community/attacks/Session_fixation" target="_blank" rel="noopener noreferrer">会话固定攻击</a> 对您的应用程序造成危害。</p>
<p>Laravel 在身份验证过程中，如果您使用 Laravel 的 <a href="https://learnku.com/docs/laravel/11.x/starter-kits" target="_blank" rel="noopener noreferrer">应用程序启步套件</a> 或 <a href="https://learnku.com/docs/laravel/11.x/fortify" target="_blank" rel="noopener noreferrer">Laravel Fortify</a>，会自动重新生成会话 ID。然而，如果您需要手动重新生成会话 ID，可以使用 <code v-pre>regenerate</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">regenerate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果您需要在一条语句中重新生成会话 ID 并且清除会话中的所有数据，可以使用 <code v-pre>invalidate</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">invalidate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="会话阻塞" tabindex="-1"><a class="header-anchor" href="#会话阻塞"><span>会话阻塞</span></a></h2>
<blockquote>
<p><strong>警告</strong><br>
要利用会话阻塞，您的应用程序必须使用支持 <a href="https://learnku.com/docs/laravel/11.x/cache#atomic-locks" target="_blank" rel="noopener noreferrer">原子锁（atomic locks）</a> 的缓存驱动。目前，这些缓存驱动包括 <code v-pre>memcached</code>、<code v-pre>dynamodb</code>、<code v-pre>redis</code>、<code v-pre>database</code>、<code v-pre>file</code> 和 <code v-pre>array</code> 驱动。此外，您不能使用 <code v-pre>cookie</code> 会话驱动。</p>
</blockquote>
<p>默认情况下，Laravel 允许使用相同会话的请求并发执行。例如，如果您使用 JavaScript HTTP 库向应用程序发起两个 HTTP 请求，它们将同时执行。对于许多应用程序来说，这并不是问题；然而，在一小部分应用程序中，这些应用程序同时向两个不同的应用程序端点发起并写入会话数据时，可能会出现会话数据丢失的情况。</p>
<p>为了抑制这种情况，Laravel 提供了一种功能，允许您限制特定会话的并发请求。要开始使用该功能，您可以简单地在路由定义中链式调用 <code v-pre>block</code> 方法。在这个示例中，对 <code v-pre>/profile</code> 端点的传入请求会获取一个会话锁定。当持有此锁时，任何对 <code v-pre>/profile</code> 或 <code v-pre>/order</code> 端点的传入请求（它们共享相同的会话 ID）将等待第一个请求完成执行后再继续它们的执行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/profile'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">block</span><span class="token punctuation">(</span><span class="token variable">$lockSeconds</span> <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token variable">$waitSeconds</span> <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/order'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">block</span><span class="token punctuation">(</span><span class="token variable">$lockSeconds</span> <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token variable">$waitSeconds</span> <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>block</code> 方法接受两个可选参数。 <code v-pre>block</code> 方法接受的第一个参数是 Session 锁应该持有的最大秒数，超过这个时间后锁将被释放。当然，如果请求在这个时间之前执行完毕，锁会更早释放。</p>
<p><code v-pre>block</code> 方法接受的第二个参数是请求在尝试获取 Session 锁时应该等待的秒数。如果请求在给定的秒数内无法获得 Session 锁，将抛出 <code v-pre>Illuminate\Contracts\Cache\LockTimeoutException</code> 异常。</p>
<p>如果这两个参数都没有传递，锁将最多持有 10 秒，而请求在尝试获取锁时最多等待 10 秒：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/profile'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">block</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="添加自定义-session-驱动程序" tabindex="-1"><a class="header-anchor" href="#添加自定义-session-驱动程序"><span>添加自定义 Session 驱动程序</span></a></h2>
<h3 id="实现驱动程序" tabindex="-1"><a class="header-anchor" href="#实现驱动程序"><span>实现驱动程序</span></a></h3>
<p>如果现有的 Session 驱动程序都不能满足您应用程序的需求，Laravel 允许您编写自己的 Session 处理程序。您的自定义 Session 驱动程序应该实现 PHP 内置的 <code v-pre>SessionHandlerInterface</code> 接口。这个接口只包含几个简单的方法。一个简单的 MongoDB 实现大致如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Extensions</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">MongoSessionHandler</span> <span class="token keyword">implements</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>SessionHandlerInterface</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">open</span><span class="token punctuation">(</span><span class="token variable">$savePath</span><span class="token punctuation">,</span> <span class="token variable">$sessionName</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">read</span><span class="token punctuation">(</span><span class="token variable">$sessionId</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">write</span><span class="token punctuation">(</span><span class="token variable">$sessionId</span><span class="token punctuation">,</span> <span class="token variable">$data</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">destroy</span><span class="token punctuation">(</span><span class="token variable">$sessionId</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">gc</span><span class="token punctuation">(</span><span class="token variable">$lifetime</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>技巧</strong><br>
Laravel 并没有为您的扩展提供专门的目录。您可以自由地将它们放在任何您喜欢的地方。在这个例子中，我们创建了一个 <code v-pre>Extensions</code> 目录来存放 <code v-pre>MongoSessionHandler</code> 。</p>
</blockquote>
<p>由于这些方法的目的不容易理解，让我们快速介绍一下每个方法的作用:</p>
<ul>
<li><code v-pre>open</code> 方法通常用于基于文件的 Session 存储系统。因为 Laravel 自带了一个 <code v-pre>file</code> Session 驱动程序，所以你很少需要在这个方法中添加任何东西。您可以简单地将此方法保留为空。</li>
<li>与 <code v-pre>open</code> 方法一样，<code v-pre>close</code> 方法通常也可以忽略。对于大多数驱动来说，这是不需要的。</li>
<li><code v-pre>read</code> 方法应该返回与给定 <code v-pre>$sessionId</code> 相关联的 Session 数据的字符串版本。当在驱动程序中检索或存储会话数据时，不需要进行任何序列化或其他编码，因为 Laravel 将为您执行序列化。</li>
<li><code v-pre>write</code> 方法应该将给定的 <code v-pre>$data</code> 字符串与 <code v-pre>$sessionId</code> 关联到一些持久存储系统，例如 MongoDB 或您选择的其他存储系统。同样，你不应该执行任何序列化 - Laravel 已经为你处理过了。</li>
<li><code v-pre>destroy</code> 方法应该从持久存储中删除与 <code v-pre>$sessionId</code> 相关的数据。</li>
<li><code v-pre>gc</code> 方法应该销毁比给定的 <code v-pre>$lifetime</code> 更早的所有 Sessopm 数据，这是一个 UNIX 时间戳。对于像 Memcached 和 Redis 这样的自动过期系统，这个方法可能是空的。</li>
</ul>
<h3 id="注册驱动程序" tabindex="-1"><a class="header-anchor" href="#注册驱动程序"><span>注册驱动程序</span></a></h3>
<p>一旦你的驱动程序被实现，你就可以在 Laravel 上注册它了。要为 Laravel 的会话后端添加额外的驱动程序，你可以使用 <code v-pre>Session</code> <a href="https://learnku.com/docs/laravel/11.x/facades" target="_blank" rel="noopener noreferrer">门面</a>提供的 <code v-pre>extend</code> 方法。你应该从<a href="https://learnku.com/docs/laravel/11.x/providers" target="_blank" rel="noopener noreferrer">服务提供者</a>的 <code v-pre>boot</code> 方法中调用 <code v-pre>extend</code> 方法。您可以从现有的 <code v-pre>App\Providers\AppServiceProvider</code> 中执行此操作，也可以创建一个全新的提供者:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Providers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Extensions<span class="token punctuation">\</span>MongoSessionHandler</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Application</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Session</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>ServiceProvider</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">SessionServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 注册任何应用程序服务</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">register</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 引导任何应用程序服务</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Session</span><span class="token operator">::</span><span class="token function">extend</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mongo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Application</span> <span class="token variable">$app</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token comment">// 返回 SessionHandlerInterface 的实现...</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">MongoSessionHandler</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦 session 驱动程序注册完毕，可以使用 <code v-pre>SESSION_DRIVER</code> 环境变量或在应用程序的 <code v-pre>config/session.php</code> 配置文件中将 <code v-pre>mongo</code> 驱动程序指定为应用程序的 session 驱动程序。</p>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/sessionmd/16667" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/se...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/sessionmd/16667" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/se...</a></p>
</blockquote>
</div></template>


